import {
    addSettlement,
    getMaxSerialNo,
    checkEngineeringName
} from "@/views/wy-budget/budget-settlement-review/api/budget-settlement-review.js";
import uploadMedia from "~/components/upload-media.vue";

export default {
    components: {
        uploadMedia
    },
    data() {

        let validatorEngineeringName = async (rule, value, callback) => {
            if (value == '') {
                callback(new Error('请输入工程名称'));
            }
            await checkEngineeringName(value,this.formObj.costPriceType).then(respData => {
                let result = respData.data.data;
                if (result == false) {
                    callback(new Error('工程名称重复'));
                }
            });
        };

        return {
            //上传文件相关
            busId: "",
            modelType: "settlement-review",
            url: "/admin/sys-file/upLodeFileEdit",

            //弹出框 初始时隐藏
            dialogVisible: false,
            formObj: {
                seReId: "",
                engineeringName: "",
                buildUnit: "",
                auditUnit: "",
                constructionUnit: "",
                declarePrice: 0,
                auditPrice: 0,
                unitPrice: 0,
                totalPrice: 0,
                totalPriceStr: "",
                prepareUser: "",
                reviewer: "",
                approver: "",
                qualificationCardNo: "",
                completionDate: "",
                serialNo: "",
                subtract:"",
                subtractRate:"",
                remarks: "",
                //类型 0 预算 1 结算
                costPriceType: 1,
            },
            rules: {
                subtract: [
                    {
                        required: true,
                        message: "请输入核减",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                subtractRate: [
                    {
                        required: true,
                        message: "请输入核减率",
                        trigger: "blur"
                    }
                ],
                engineeringName: [
                    {required: true, message: '请输入单位名称', trigger: 'blur'},
                    {validator: validatorEngineeringName, trigger: 'blur'},
                ],
                buildUnit: [
                    {
                        required: true,
                        message: "请输入建设单位",
                        trigger: "blur"
                    }
                ],
                auditUnit: [
                    {
                        required: true,
                        message: "请输入审核单位",
                        trigger: "blur"
                    }
                ],
                constructionUnit: [
                    {
                        required: true,
                        message: "请输入施工单位",
                        trigger: "blur"
                    }
                ],
                declarePrice: [
                    {
                        required: true,
                        message: "请输入申报价格",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                auditPrice: [
                    {
                        required: true,
                        message: "请输入审核价格",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                unitPrice: [
                    {
                        required: true,
                        message: "请输入单位造价",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                totalPrice: [
                    {
                        required: true,
                        message: "请输入工程总造价",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                totalPriceStr: [
                    {
                        required: true,
                        message: "请输入工程总造价(大写)",
                        trigger: "blur"
                    }
                ],
                prepareUser: [
                    {
                        required: true,
                        message: "请输入编制人",
                        trigger: "blur"
                    }
                ],
                reviewer: [
                    {
                        required: true,
                        message: "请输入审核人",
                        trigger: "blur"
                    }
                ],
                approver: [
                    {
                        required: true,
                        message: "请输入批准人",
                        trigger: "blur"
                    }
                ],
                qualificationCardNo: [
                    {
                        required: true,
                        message: "请输入资格证号",
                        trigger: "blur"
                    }
                ],
                completionDate: [
                    {
                        required: true,
                        message: "请输入完成日期",
                        trigger: "blur"
                    }
                ],
                // serialNo: [
                //     {
                //         required: true,
                //         message: "请输入编号",
                //         trigger: "blur"
                //     }
                // ],

            }
        };
    },
    //载入事件
    mounted: function () {
        /**
         * 自动生产编号
         */
        let date = new Date();
        let year = date.getFullYear(); // 年
        let month = date.getMonth() + 1; // 月
        if (month < 10) {
            month = "0" + month;
        }
        let day = date.getDate(); // 日
        if (day < 10) {
            day = "0" + day;
        }
        //获取当天最大批次号
        // getMaxSerialNo(this.formObj.costPriceType).then(rep => {
        //     console.log(rep.data)
        //     if (rep.data.data == null || rep.data.data == "") {
        //         this.formObj.serialNo = "WYBJS" + year + "" + month + "" + day + "001";
        //     } else {
        //         let maxSerialNo = rep.data.data;
        //         let num = parseInt(maxSerialNo) + 1;
        //         if (num < 10) {
        //             num = "00" + num;
        //         }
        //         if (num > 10 && num < 100) {
        //             num = "0" + num;
        //         }
        //         this.formObj.serialNo = "WYBYS" + year + "" + month + "" + day + "" + num;
        //     }
        // }).catch(error => {
        //     this.$message({
        //         type: "error",
        //         message: "编号生产失败"
        //     });
        // }).finally(() => (this.loading = false));
    },
    created() {

    },
    methods: {
        /**
         * 保存按钮 触发文件上传
         */
        sendData() {
            this.$refs.filePlugIn.submit(this.modelType);
        },

        /**
         * 保存表单信息
         * @returns {Promise<void>}
         */
        saveEntity(val) {
            this.loading = true;
            if (val != null) {
                this.formObj.seReId = val.data;
            }
            this.$refs.ruleForm.validate((valid)=> {
                if (valid) {
                    addSettlement(this.formObj).then(data => {
                        this.$message({
                            type: "success",
                            message: "操作成功"
                        });
                        //操作完成 跳转页面
                        this.loading = false;
                        this.$router.push(
                            "/wy-operate/repair/review-list"
                        );
                    }).catch(error => {
                        this.$message({
                            type: "error",
                            message: "操作失败"
                        });
                    }).finally(() => (this.loading = false));
                }
            })

        },

        //返回
        goBack() {
            this.$router.push("/wy-operate/repair/review-list");
        },

    }
};
